var express = require('express');
var router = express.Router();
var path = require('path');
var fs= require('fs');
var Post = require('../dao/post');
var User = require('../dao/user');
var uid = require('../utils/uuid');//用于生成id

var data = null;
//读取数据
fs.readFile(path.join(__dirname, 'citylist.json'), {encoding:'utf-8'}, function (err,bytesRead) {
    if (err) throw err;
    data=JSON.parse(bytesRead);
    console.log(data);
})

var sess;
/* GET home page. */
router.get('/', function(req, res, next) {
    //var html = path.normalize(__dirname + '/../views/index.html');
    //res.sendFile(html);
    sess = req.session;
    console.log("currentUser: "+ sess.user)
    res.render('index');
});

router.get('/school*', function(req, res, next) {
    //var html = path.normalize(__dirname + '/../views/index.html');
    //res.sendFile(html);
    res.render('index');
});

router.get('/comment/?', function(req, res) {
    var condition =
        'cityname="'+req.query.city+
        '" and schoolname="'+req.query.school+
        '" and campusname="'+req.query.campus+
        '" and gatename="'+req.query.gate+ '"';
    var sql = "select comment,user,time from comments where "+ condition + " order by id DESC;"
    Post.get(sql, getComent);
    function getComent(result) {
        res.send(result);
    }
})

router.post('/comment', function(req, res) {
    sess = req.session;
    if(!sess.user) {
        res.send({loginStatus: 0});
        return;
    };
    var params = [req.body.city, req.body.school, req.body.campus, req.body.gate, req.body.comment, req.body.user, req.body.time];
    var condition =
        'cityname="'+req.body.city+
        '" and schoolname="'+req.body.school+
        '" and campusname="'+req.body.campus+
        '" and gatename="'+req.body.gate+ '"';
    Post.addcomment(params, get);
    function get() {
        var sql = "select comment,user,time from comments where "+ condition + " order by id DESC;"
        Post.get(sql, getComent);
        function getComent(result) {
            res.send(result);
        }
    }
})

router.get('/like/?', function(req, res) {
    var condition =
        'cityname="'+req.query.city+
        '" and schoolname="'+req.query.school+
        '" and campusname="'+req.query.campus+
        '" and gatename="'+req.query.gate + '"';
    var sql = "select likenum from schools where "+ condition + ";"
    Post.get(sql, getLike);
    function getLike(result) {
        console.log('/like/? :'+result)
        res.send(result[0]);
    }
})

router.get('/likestatus/?', function(req, res) {
    var condition =
        'cityname="'+req.query.city+
        '" and schoolname="'+req.query.school+
        '" and campusname="'+req.query.campus+
        '" and gatename="'+req.query.gate+
        '" and user="'+ req.query.user+'"';
    //判断是否点过赞
    var sql = 'select id from likes where ' + condition + ';'
    Post.get(sql, next);
    function next(result) {
        if(result[0]) {
            //先查询该用户是否已经赞过
            res.send({likeStatus: 1});
        }
        else {
            res.send({likeStatus: 0})
        }
    }
})

router.post('/like', function(req, res) {
    sess = req.session;
    //console.log(sess.user)
    if(!sess.user) {
        res.send({loginStatus: 0});
        return;
    };
    var condition =
        'cityname="'+req.body.city+
        '" and schoolname="'+req.body.school+
        '" and campusname="'+req.body.campus+
        '" and gatename="'+req.body.gate + '"';
    var params = [req.body.city, req.body.school, req.body.campus, req.body.gate, req.body.user, req.body.time];
    Post.update("likenum", condition, getLike);
    Post.addLike(params);
    function getLike(result) {
        res.send(result[0]);
    }
})

//获取城市、学校信息
router.get('/getinfo*', function(req, res, next) {
    if(req.query.citylist == 1) {
        var sql = "SELECT cityname, sum(likenum) as total FROM schools group by cityname order by total desc;";
        Post.get(sql, get);
        //res.send(data[0]);
    }
    else if(req.query.gate) {
        var city = req.query.city;
        var school = req.query.school;
        var campus = req.query.campus;
        var gate = req.query.gate;
        var sql = "SELECT gatename, pid, heading, pitch FROM schools where cityname='"+
            city +"' and schoolname='"+ school +"' and campusname='"+ campus +"' and gatename='"+ gate+"';";
        Post.get(sql, get);
    }
    else if(req.query.campus) {
        var city = req.query.city;
        var school = req.query.school;
        var campus = req.query.campus;
        var sql = "SELECT gatename, alsoname, pid, heading, pitch, story, likenum FROM schools where cityname='"+
            city +"' and schoolname='"+ school +"' and campusname='"+ campus +"' order by likenum desc;";
        Post.get(sql, get);
    }
    else if(req.query.school) {
        var city = req.query.city;
        var school = req.query.school;
        var sql = "SELECT campusname, sum(likenum) as total FROM schools where cityname='"+
                  city +"' and schoolname='"+ school +"' group by campusname order by total desc;";
        Post.get(sql, get);
    }
    else if(req.query.city) {
        var city = req.query.city
        var sql = "SELECT schoolname, sum(likenum) as total FROM schools where cityname='"+ city +"' group by schoolname order by total desc;";
        Post.get(sql, get);
    }
    function get(result) {
        console.log(result)
        res.send(result);
    }
});

//regist routers
router.get('/regist', function(req, res, next) {
    res.render('regist');
})

router.post('/regist', function(req, res, next) {
    User.save(req.body, get)
    function get(result) {
        res.send(result);
    }
})

//login routers
router.get('/login', function(req, res, next) {
    res.render('login');
})

router.post('/login', function(req, res, next) {
    sess = req.session;
    User.get(req.body, get);
    function get(result) {
        if(result.loginStatus == 1) {sess.user = req.body.username;}
        else sess.user = null;
        res.send(result);
    }
})


router.get('/islogin', function(req, res, next) {
    sess = req.session;
    if(sess.user) {
        res.send({loginStatus: 1, username: sess.user})
    }
    else {
        res.send({loginStatus: 0, username: null})
    }
})

router.get('/logout', function(req, res, next) {
    sess = req.session;
    sess.user = null;
    res.redirect('/');
})

module.exports = router;
